package net.piencak.phonetools.eurocoins;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class Wallet {

	private RecordStore rs;
	private static final int version = 1;
	
	public static final String RS_NAME = "EuroCoinCollector";
	
	private boolean[][] wallet = new boolean[Gallery.COUNTRIES_COUNT][Gallery.COINS_COUNT];
	
	public void load() {
		try {
			rs = RecordStore.openRecordStore(RS_NAME, true);
			int rsVersion = 0;
			if (rs.getNumRecords() >= 2) {
				RecordEnumeration records = rs.enumerateRecords(null, null, false);
				int idVersion = 1; //records.nextRecordId();
				int idWallet = 2; //records.nextRecordId();
				byte b[] = rs.getRecord(idVersion);
				String versionStr = new String(b, 0, b.length);
				try {
					rsVersion = Integer.parseInt(versionStr);
					if (rsVersion != version) {
						System.out.println("Wallet version differs.");
					}
				} catch (NumberFormatException e) {
					System.out.println("Wrong version format.");
				}
				b = rs.getRecord(idWallet);
//				String walletStr = new String(b, 0, b.length);
				if (true || b.length == (Gallery.COUNTRIES_COUNT) * Gallery.COINS_COUNT) {
					for (int i = 1; i <= Gallery.COUNTRIES_COUNT; i++) {
						for (int j = 0; j < Gallery.COINS_COUNT; j++) {
							wallet[i-1][j] = b[(i-1)*Gallery.COINS_COUNT + j] == 1;
						}
					}
				} else {
					System.out.println("Stored data inconsistent.");
				}
				rs.closeRecordStore();
			} else {
				System.out.println("Record store empty. First time used?");
			}
		} catch (RecordStoreFullException e) {
			System.out.println("Record store full");
		} catch (RecordStoreNotFoundException e) {
			System.out.println("Record store not found");
		} catch (RecordStoreException e) {
			System.out.println("Record store exception: " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public int getVersion() {
		return version;
	}
	
	public boolean isOwned(int country, int coin) {
		return wallet[country-1][coin];
	}

	public void setOwned(int country, int coin, boolean owned) {
		wallet[country-1][coin] = owned;
	}

	public void save() {
		try {
			rs = RecordStore.openRecordStore(RS_NAME, true);
			System.out.println("records:" + rs.getNumRecords());
			
			int idVersion = 1;
			int idWallet = 2;
			if (rs.getNumRecords() > 0) {
				RecordEnumeration records = rs.enumerateRecords(null, null, false);
				idVersion = 1;  //records.nextRecordId();
				idWallet = 2; //records.nextRecordId();
				byte bytes[] = Integer.toString(version).getBytes();
				rs.setRecord(idVersion, bytes, 0, bytes.length);
				bytes = new byte[Gallery.COUNTRIES_COUNT * Gallery.COINS_COUNT];
				for (int i = 1; i <= Gallery.COUNTRIES_COUNT; i++) {
					for (int j = 0; j < Gallery.COINS_COUNT; j++) {
						bytes[(i-1)*Gallery.COINS_COUNT + j] = (wallet[i-1][j] ? (byte)1 : (byte)0);
					}
				}
				rs.setRecord(idWallet, bytes, 0, bytes.length);
			} else {
				byte bytes[] = Integer.toString(version).getBytes();
				int k = rs.addRecord(bytes, 0, bytes.length);
				System.out.println("added record " + k);
				bytes = new byte[Gallery.COUNTRIES_COUNT * Gallery.COINS_COUNT];
				for (int i = 1; i <= Gallery.COUNTRIES_COUNT; i++) {
					for (int j = 0; j < Gallery.COINS_COUNT; j++) {
						bytes[(i-1)*Gallery.COINS_COUNT + j] = (wallet[i-1][j] ? (byte)1 : (byte)0);
					}
				}
				k = rs.addRecord(bytes, 0, bytes.length);
				System.out.println("added record " + k);
			}
			rs.closeRecordStore();
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void destroy() {
		save();
		try {
			rs.closeRecordStore();
		} catch (RecordStoreNotOpenException e) {
			System.out.println("Record store not open");
		} catch (RecordStoreException e) {
			System.out.println("Record store exception: " + e.getMessage());
			e.printStackTrace();
		}	
	}
}
